/**
 * axios请求封装
 */
import axios from "axios";
import { Toast } from "vant";
import baseURL from "./config";

const instance = axios.create({
  baseURL,
  timeout: 10 * 1000,
});

var toast;
// 设置拦截器
instance.interceptors.request.use(
  (config) => {
    toast = Toast.loading({
      duration: 0, // 持续展示 toast
      message: "加载中...",
    });
    if (config.method.toLowerCase() == "get") {
      config.params = config.data;
    }
    return config;
  },
  (err) => {
    toast.clear();
    Promise.reject(err);
  }
);

instance.interceptors.response.use(
  (repsonse) => {
    toast.clear();
    return repsonse.data;
  },
  (err) => {
    toast.clear();
    Promise.reject(err);
  }
);

export default instance;
